package br.com.sysdrive.controller.Cliente;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import br.com.sysdrive.DAO.ClienteDAOImpl;
import br.com.sysdrive.DAO.GenericDAO;
import br.com.sysdrive.DAO.UsuarioDAOImpl;
import br.com.sysdrive.model.Cliente;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author WillianFrias
 */
public class AlterarCliente extends HttpServlet {

    public static Date converterData(String data) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
        if (data == null || data.trim().equals("")) {
            return null;
        } else {
            Date date = fmt.parse(data);
            return date;
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        
        Cliente cliente = new Cliente();
        cliente.setIdUsuario(Integer.parseInt(request.getParameter("idusuario")));
        cliente.setNomeUsuario(request.getParameter("nomeusuario"));
        cliente.setDataNascimentoUsuario(converterData(request.getParameter("datanascimentousuario")));
        cliente.setSexoUsuario(request.getParameter("sexousuario"));
        cliente.setRgIeUsuario(request.getParameter("rgieusuario"));
        cliente.setCpfCnpjUsuario(request.getParameter("cpfcnpjusuario"));
        cliente.setRuaEnderecoUsuario(request.getParameter("ruaenderecousuario"));
        cliente.setNumeroEnderecoUsuario(Integer.parseInt(request.getParameter("numeroenderecousuario")));
        cliente.setBairroUsuario(request.getParameter("bairrousuario"));
        cliente.setComplementoUsuario(request.getParameter("complementousuario"));
        cliente.setCidadeUsuario(request.getParameter("cidadeusuario"));
        cliente.setCepUsuario(request.getParameter("cepusuario"));
        cliente.setUfUsuario(request.getParameter("ufusuario"));
        cliente.setEmailUsuario(request.getParameter("emailusuario"));
        cliente.setTelefoneUsuario(request.getParameter("telefoneusuario"));
        cliente.setCelularUsuario(request.getParameter("celularusuario"));
        cliente.setDataCadastroUsuario(converterData(request.getParameter("datacadastrousuario")));
        cliente.setStatusUsuario(request.getParameter("statususuario"));
        cliente.setLoginUsuario(request.getParameter("loginusuario"));
        cliente.setSenhaUsuario(request.getParameter("senhausuario"));
        cliente.setProfissaoCliente(request.getParameter("profissaocliente"));
        cliente.setLocalTrabalhoCliente(request.getParameter("localtrabalhocliente"));
        cliente.setRendaCliente(Double.parseDouble(request.getParameter("rendacliente")));

        String msg = "";
        try {
            GenericDAO daoCliente = new ClienteDAOImpl();
            if (daoCliente.alterar(cliente)) {
                UsuarioDAOImpl daoUsuario = new UsuarioDAOImpl();
                if (daoUsuario.alterar(cliente)) {
                    msg = "Cliente alterado com Sucesso!";
                } else {
                    msg = "Dados inválidos – Verifique dados obrigatórios";
                }
            }
            request.setAttribute("mensagem", msg);
            request.getRequestDispatcher("ListarCliente").forward(request, response);
        } catch (Exception ex) {
            System.out.println("Erro ao Alterar dados do Cliente! " + ex.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(AlterarCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(AlterarCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
